package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Map;

/**
 * @date 2025/9/26
 */
//@RestController
 @Controller
 @RequestMapping("/param")
public class ParamController {


    @ResponseBody
    @RequestMapping("/f1")
    public String f1(HttpServletRequest request , HttpServletResponse response , HttpSession session){
         ServletContext application = request.getServletContext();
         ServletContext application2 = session.getServletContext();
         System.out.println(application);
         System.out.println(application2);
         return "<h1 style='color:aqua;'>Hello World</h1>";
    }

    @ResponseBody
    @RequestMapping("/f2")
    public String f2(ServletContext application){
        return "<h1 style='color:red;'>Hello World f2</h1>";
    }

    @ResponseBody
    @RequestMapping("/f3")
    public String f3(InputStream in , OutputStream out){
        return "f3";
    }

    @ResponseBody
    @RequestMapping("f4")
    public String f4(Reader in , Writer out){
        return "f4";
    }

    @RequestMapping("/f6")
    public String f6(Model model , Map map , ModelMap modelMap){
        model.addAttribute("model","Hello Model");
        map.put("map","Hello Map");
        //ModelMap即可以使用Model的方法，也可以使用Map的方法
        model.addAttribute("modelMap","Hello ModelMap");
        modelMap.put("modelMap2","Hello ModelMap2");
//        return "model";
        return "redirect:/showModel";
        //在使用重定向之后，作用域中数据丢失，其效果类似与request用法
        //但是数据又没有完全丢失，而是在地址栏以参数的形式存储了
    }

//    @RequestMapping("/f7")
//    public void f7(@RequestParam(value = "username" , required = false)){}

}
